﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WcfBackend;

public partial class Shoppinglist : System.Web.UI.Page
{
    private static WcfFoodAndAllergiesClient client;
    HttpCookie listCookie = new HttpCookie("cookiesName");

    protected void Page_Load(object sender, EventArgs e)
    {
        
        client = new WcfFoodAndAllergiesClient();
        if (Session["products"] != null)
        {
            listFromRecipe();
            Session["products"] = null;
        }

        



    }

    protected void MoveLeft_Click(object sender, EventArgs e)
    {
        List<ListItem> output = new List<ListItem>();



        foreach (ListItem item in HiddenShoppingList.Items)
        {
            if (item.Selected == true)
            {
                Productlist.Items.Add(item);
                output.Add(item);
                item.Selected = false;

            }

        }
        foreach (ListItem item in output)
        {
            HiddenShoppingList.Items.Remove(item);
        }
        DropDownTypes_SelectedIndexChanged(sender, e);


    }
    protected void MoveRight_Click(object sender, EventArgs e)
    {
        List<ListItem> output = new List<ListItem>();
        List<Product> slist = new List<Product>();
        foreach (ListItem li in HiddenShoppingList.Items)
        {
            Product p = client.GetExactProductByName(li.Text);
            slist.Add(p);
        }
      
        foreach (ListItem item in Productlist.Items)
        {
            if (item.Selected == true)
            {
                Product p = client.GetExactProductByName(item.Text);
                slist.Add(p);
                HiddenShoppingList.Items.Add(item);
                output.Add(item);
            }

        }
        foreach (ListItem item in output)
        {
            Productlist.Items.Remove(item);
        }
      


    }
    private void SortShoppingList()
    {
        List<Product> chosen = new List<Product>();
        foreach (ListItem item in HiddenShoppingList.Items)
        {
            chosen.Add(client.GetExactProductByName(item.Text));
        }
        var sorted = chosen.OrderBy(i => i.Name).ToList();
        HiddenShoppingList.Items.Clear();
        foreach (Product a in sorted)
        {
            HiddenShoppingList.Items.Add(a.Name);
        }
    }
    private void SortProduct()
    {

        List<Product> shopList = new List<Product>();
        List<Product> remaining = new List<Product>();
        foreach (ListItem item in HiddenShoppingList.Items)
        {
            shopList.Add(client.GetExactProductByName(item.Text));
        }
        if (shopList.Count > 0)
        {
            remaining = client.GetRemaningProducts(shopList.ToArray()).ToList();
        }
        else
        {
            remaining = client.SelectAllProduct().ToList();
        }

        Productlist.Items.Clear();
        foreach (Product p in remaining)
        {
            Productlist.Items.Add(p.Name);
        }



    }

    private void QuicSortProducts()
    {

        List<Product> ext = new List<Product>();
        foreach (ListItem item in Productlist.Items)
        {
            ext.Add(client.GetExactProductByName(item.Text));
        }
        var sorted = ext.OrderBy(i => i.Name).ToList();

        foreach (Product a in sorted)
        {
            Productlist.Items.Add(a.Name);
        }
    }

    public void listFromRecipe()
    {
        var myObjectInstance = (List<Product>)Session["products"];
        List<Product> ingredientList = myObjectInstance;
        foreach (Product p in ingredientList)
        {
            ListItem item = new ListItem();
            item.Text = p.Name;
            HiddenShoppingList.Items.Add(item);

         


        }

    }
    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // javascript function to call on row-click event
            e.Row.Attributes.Add("onClick", "javascript:void SelectRow(this);");
        }
    }
    protected void DropDownTypes_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Product> searchWordList = new List<Product>();
        List<Product> retList = new List<Product>();
        string name = searchWord.Text.Trim();
        string type = DropDownTypes.SelectedValue;

        Productlist.Items.Clear();
        if(name.Length>0)
        {
            if (DropDownTypes.SelectedValue != "Type")
            {

                retList = client.GetProductByNameAndType(name, type).ToList();
               
                
            }
            else
            {
                retList = client.GetProductByName(name).ToList() ;
            }


        }
        else
        {
            if (DropDownTypes.SelectedValue != "Type")
            {
                retList = client.GetProductByType(type).ToList();
            }
            else
            {
                retList = client.SelectAllProduct().ToList();
            }
        }

        foreach(Product p in retList)
        {
            ListItem item = new ListItem();
            item.Text = p.Name;

            if (!HiddenShoppingList.Items.Contains(item))
            {
                Productlist.Items.Add(item);
            }
            
        }
    }

}